package com.fieldchain.ex.fa100.utils.chart.utils.data;
import android.util.Log;
import com.fieldchain.ex.fa100.utils.chart.dataset.MyCandleDataSet;
import com.fieldchain.ex.fa100.utils.chart.dataset.MyLine10DataSet;
import com.fieldchain.ex.fa100.utils.chart.dataset.MyLine5DataSet;
import com.fieldchain.ex.fa100.utils.chart.utils.zb.BOLLParse;
import com.github.mikephil.charting.data.CandleData;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/10/5
 */
public class DataParseCandle {


    private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

    public  CombinedData getCandData(List<List<String>> mList,int type) {
        if(mList.size()>3) {
            long t1 = Long.parseLong(mList.get(0).get(0));
            long t2 = Long.parseLong(mList.get(1).get(0));
            long t3 = Long.parseLong(mList.get(2).get(0));
            int day = 60 * 60 * 24*1000;
            if ((t2 - t1) > day && (t3 - t2) > day) {
                sdf = new SimpleDateFormat("MM-dd");
            } else {
                sdf = new SimpleDateFormat("HH:mm");
            }
        }
        CombinedData combinedData=new CombinedData();
        List<CandleEntry> candleValues = new ArrayList<>();
        List<Entry> lineValue5 = new ArrayList<>();
        List<Entry> lineValue10 = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            List<String> k = mList.get(i);
            Date d = new Date(Long.parseLong(k.get(0)));//毫秒
            float high = Float.parseFloat(k.get(1));
            float open = Float.parseFloat(k.get(2));
            float low = Float.parseFloat(k.get(3));
            float close = Float.parseFloat(k.get(4));
            String time=sdf.format(d);


            lineValue5.add(new Entry(i,getMA(i,7,mList)));
            lineValue10.add(new Entry(i,getMA(i,30,mList)));
            candleValues.add(new CandleEntry(i, high,low, open, close,time));
        }


        LineData lineData=new LineData();
        MyLine5DataSet lineDataSet5=new MyLine5DataSet(lineValue5,"5m");
        MyLine10DataSet lineDataSet10=new MyLine10DataSet(lineValue10,"10m");

        for(int i=0;i<lineValue5.size();i++){
            Log.e("list5  "+i+":  ",lineValue5.get(i).getY()+"");
            Log.e("list10  "+i+":  ",lineValue10.get(i).getY()+"");
        }
        if(type!=2) {
            lineData.addDataSet(lineDataSet5);
            lineData.addDataSet(lineDataSet10);
        }else {
            lineData=new BOLLParse().getLineData(mList);
        }

        MyCandleDataSet candleSet=new MyCandleDataSet(candleValues,"candle");
        CandleData candleData = new CandleData(candleSet);

        combinedData.setData(candleData);
        combinedData.setData(lineData);

        return combinedData;
    }

    public  List<Entry> getLineMaList(List<List<String>> mList,int ma) {
        Log.e("mac:"," ma:"+ma);
        List<Entry> lineValue = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            lineValue.add(new Entry(i,getMA(i,ma,mList)));
        }
        return lineValue;
    }

    private float getMA(int index, int ma,List<List<String>> mList) {
        int count = 1;
        float sum = Float.parseFloat(mList.get(index).get(4));
        while (count < ma) {
            if (--index < 0) {
                break;
            }
            sum += Float.parseFloat(mList.get(index).get(4));
            count++;
        }
        return sum / count;
    }
}
